<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="支付订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payOrderNo">
              <a-input-number v-model="form.payOrderNo" placeholder="请输入支付订单号" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="uid">
              <a-input-number v-model="form.uid" placeholder="请输入用户ID" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="1=商城订单，2=充值会员，3=充值钱包，4=预售单-定金，5=预售单-尾款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-input-number v-model="form.type" placeholder="请输入1=商城订单，2=充值会员，3=充值钱包，4=预售单-定金，5=预售单-尾款" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payAmount">
              <a-input-number v-model="form.payAmount" placeholder="请输入支付金额" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付渠道:0=余额支付,1=支付宝支付,2=微信支付,3=公众号支付,4=小程序支付,5=apple pay,6=数字资产支付
" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payChannel">
              <a-input-number v-model="form.payChannel" placeholder="请输入支付渠道:0=余额支付,1=支付宝支付,2=微信支付,3=公众号支付,4=小程序支付,5=apple pay,6=数字资产支付
" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="第三方支付流水号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradeNo">
              <a-input v-model="form.tradeNo" placeholder="请输入第三方支付流水号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="0:待支付,1:支付成功,2:支付失败3.关闭" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input-number v-model="form.status" placeholder="请输入0:待支付,1:支付成功,2:支付失败3.关闭" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建时间" v-model="form.createTime" :trigger-change="true" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateTime">
              <j-date placeholder="请选择修改时间" v-model="form.updateTime" :trigger-change="true" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="额外参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ext">
              <a-input v-model="form.ext" placeholder="请输入额外参数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: 'OmsPayOrderForm',
    components: {
      JFormContainer,
      JDate,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: {
            payOrderNo:'',
            uid:'',
            type:'',
            payAmount:'',
            payChannel:'',
            tradeNo:'',
            status:'',
            createTime:'',
            updateTime:'',
            ext:'',
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        rules: {
          payOrderNo:  [
              { required: true, message: '请输入支付订单号!'},
            ],
          payAmount:  [
              { required: true, message: '请输入支付金额!'},
            ],
          status:  [
              { required: true, message: '请输入0:待支付,1:支付成功,2:支付失败3.关闭!'},
            ],
        },
        url: {
          add: "/mallBase/omsPayOrder/add",
          edit: "//mallBase/omsPayOrder/edit",
          queryById: "//mallBase/omsPayOrder/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({
            payOrderNo:'',
            uid:'',
            type:'',
            payAmount:'',
            payChannel:'',
            tradeNo:'',
            status:'',
            createTime:'',
            updateTime:'',
            ext:'',
        })
      },
      edit (record) {
        this.form = Object.assign({}, record)
        this.visible = true
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.form.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign({}, this.form)
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form = row
      },
    }
  }
</script>
